package com.blb.mapper.impl;

import com.blb.mapper.DepartmentDAO;
import com.blb.pojo.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DepartmentDAOImpl implements DepartmentDAO {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Department> selectAll() {
        return jdbcTemplate.query("select * from t_department",
                new BeanPropertyRowMapper<>(Department.class));
    }

    @Override
    public Department selectById(Long id) {
        return jdbcTemplate.queryForObject("select * from t_department where id = ?",
                new BeanPropertyRowMapper<>(Department.class),id);
    }

    @Override
    public void insertDepartment(Department department) {
        jdbcTemplate.update("insert into t_department(name,address) values(?,?)",
                department.getName(),department.getAddress());
    }

    @Override
    public void updateDepartment(Department department) {
        jdbcTemplate.update("update t_department set name=?,address=? where id=? ",
                department.getName(),department.getAddress(),department.getId());
    }

    @Override
    public void deleteDepartment(Long id) {
        jdbcTemplate.update("delete from t_department where id = ?",
                id);
    }
}
